package com.atguigu.demo.controller;

import com.atguigu.demo.entity.Result;
import com.atguigu.demo.entity.Tiger;
import com.atguigu.demo.service.api.TigerService;
import com.atguigu.demo.utils.DemoConstant;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "名称TigerController", description = "Tiger数据的CRUD操作")
public class TigerController {
    // 祝愿大家前程似锦~~~
    @Autowired
    private TigerService tigerService;

    @GetMapping("/tiger/list")
    @Operation(summary = "获取全部Tiger数据(不分页)")
    public Result<List<Tiger>> getTigerList() {

        try {
            List<Tiger> tigerList = tigerService.getTigerList();

            return Result.ok(tigerList);
        } catch (Exception e) {
            // throw new RuntimeException(e);
            return Result.failed(DemoConstant.GLOBAL_ERROR_CODE, e.getMessage());
        }
    }

    @DeleteMapping("/tiger/{tigerId}")
    @Operation(summary = "根据id删除一条Tiger数据")
    public Result<Void> removeTigerById(
            @Parameter(name = "tigerId", description = "老虎id", in = ParameterIn.PATH, required = true)
            @PathVariable("tigerId") Integer tigerId) {

        try {
            tigerService.removeTigerById(tigerId);

            return Result.ok();
        } catch (Exception e) {
            return Result.failed(DemoConstant.GLOBAL_ERROR_CODE, e.getMessage());
        }
    }

    @GetMapping("/tiger")
    @Operation(summary = "保存一条Tiger记录")
    public Result<Void> saveTiger(Tiger tiger) {

        try {
            tigerService.saveTiger(tiger);

            return Result.ok();
        } catch (Exception e) {
            return Result.failed(DemoConstant.GLOBAL_ERROR_CODE, e.getMessage());
        }
    }

    @GetMapping("/tiger/{tigerId}")
    @Parameters(
        value = {
            @Parameter(name = "tigerId", description = "老虎的id摸不得", in = ParameterIn.PATH, required = true)
        }
    )
    public Result<Tiger> getTigerById(@PathVariable String tigerId) {

        try {
            Tiger tiger = tigerService.getTigerById(tigerId);

            return Result.ok(tiger);
        } catch (Exception e) {
            return Result.failed(DemoConstant.GLOBAL_ERROR_CODE, e.getMessage());
        }
    }

    @PutMapping("/tiger")
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "以JSON请求体形式传入的Tiger对象")
    public Result<Void> updateTiger(@RequestBody Tiger tiger) {

        try {
            tigerService.updateTiger(tiger);

            return Result.ok();
        } catch (Exception e) {
            return Result.failed(DemoConstant.GLOBAL_ERROR_CODE, e.getMessage());
        }
    }
}
